package com.template.mongo.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.ui.Model;

import com.template.mongo.persistence.entity.Account;
import com.template.mongo.service.UserService;


@Aspect
public class UserAspect {
	private static Logger logger = Logger.getLogger(UserAspect.class);

	private UserService userDetailService;

	@AfterReturning("execution(* com.template.mongo.controller.*.*(..)) && args(model,..)")
	public void addAccountToModel(Model model) throws Throwable {
		if(userDetailService.getSecurityContext().getAuthentication() != null) {
			String userName = userDetailService.getSecurityContext()
				.getAuthentication().getName();
			
			logger.debug("Advised is trigger for " + userName);
			Account account = userDetailService.getCurrentLoggedInUser();
			if(account != null) {
				model.addAttribute("userInfo", account);
			}
		}
	}
	
	/**
	 * @param userDetailService the userDetailService to set
	 */
	public void setUserDetailService(UserService userDetailService) {
		this.userDetailService = userDetailService;
	}
}
